import http from '@/servers/http'
import type { GetSite } from './interface'
import * as Storage from '@/storage'
import { makeAutoObservable, toJS } from 'mobx'
import type { AnyObj, SiteData } from '@/types'

export default class SiteStore {
    public siteData: AnyObj = {}

    constructor() {
        makeAutoObservable(this)
    }

    /**
     * 获取站点信息
     */
    getSiteData = async () => {
        const siteData = await http<GetSite.req, GetSite.res>(
            'sites/config',
            'get',
            {},
            {
                repeatFilter: false,
            },
        )
        const { data, status } = siteData || {}
        if (status > 199 && status < 300) {
            this.updateSiteData(data || {})
        }
    }

    /**
     * 更新站点信息
     */
    updateSiteData = (params: SiteData) => {
        if (JSON.stringify(toJS(this.siteData)) !== JSON.stringify(params)) {
            this.siteData = params
            Storage.setLocalStorage('SITE_DATA', params)
        }
    }
}
